import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.Buffer;
import java.util.Scanner;

public class Test {
    public static void main1(String[] args) {
        Scanner sc = new Scanner(System.in);
        float a = sc.nextFloat();
        char b = sc.next().charAt(0);
        int x=0;
        if(a>1){
            x=(int)a;
            if(a-(int)a>0){
                x++;
            }
            x--;
        }
        if(a<=1){
            if(b=='y'){
                System.out.println(20+5);
                return;
            } else if (b == 'n') {
                System.out.println(20);
                return;
            }
        }else{
            if(b=='y'){
                System.out.println(x+20+5);
                return;
            }else if(b=='n'){
                System.out.println(x+20);
                return;
            }
        }
    }


    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] cost=new int[n];
        int[] dp=new int[n+1];
        for(int i=0;i<n;i++){
            cost[i]=in.nextInt();
        }
        for(int i=2;i<=n;i++){
            dp[i]=Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
        }
        System.out.println(dp[n]);
    }

    public static void main(String[] args) throws Throwable {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(reader.readLine());
        String[] str=reader.readLine().split(" ");
        String s1=str[0],s2=str[1];
        int prev1=-1,prev2=-1,ret=0x3f3f3f3f;
        for(int i=0;i<n;i++){
            String s=reader.readLine();
            if(s.equals(s1)){
                if(prev2!=-1){
                    ret=Math.min(ret,i-prev2);
                }
                prev1=i;
            }else if(s.equals(s2)){
                if(prev1!=-1){
                    ret=Math.min(ret,i-prev1);
                }
                prev2=i;
            }
        }
        System.out.println(ret==0x3f3f3f3f?-1:ret);
    }
}
